/******************************************************************************
** Name: common.cpp                                                          **
** Description: Common and shared definitions.                               **
**                                                                           **
** Open Source Initiative (OSI) Approved 2-clause BSD License                             **
**                                                                           **
**  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
**  All rights reserved.                                                     **
**                                                                           **
**  Redistribution and use in source and binary forms, with or without       **
**  modification, are permitted provided that the following conditions are   **
**  met:                                                                     **
**                                                                           **
**  * Redistributions of source code must retain the above copyright notice, **
**    this list of conditions and the following disclaimer.                  **
**  * Redistributions in binary form must reproduce the above copyright      **
**    notice, this list of conditions and the following disclaimer in the    **
**    documentation and/or other materials provided with the distribution.   **
**                                                                           **
**  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
**                                                                           **
******************************************************************************/
// $Id: common.cpp 146 2011-10-22 02:09:07Z gian.james $


#include "common.hpp"
#include <string>
#include <stdlib.h>     // rand_r()
#include <cstdio>
#include <cstring>


//////////////////////////////[ struct Dice
int
Dice::rnd = 0;

unsigned int
Dice::Roll()
{
    uint8 tot = 0;
    unsigned int seed = time(NULL);
    seed >>= incr();
    for (uint8 i = 0; i < rolls; i++) {
        seed <<= i;
        tot += rand_r(&seed) % num;
    }
    return (tot?tot:1);
}



#if defined (USE_MEM_MGMT)

void * operator new(size_t size)
{
	return malloc(size);
}
// Placement new default
void * operator new(size_t, void *p)
{
	return p;
}

void * operator new [] (size_t size)
{
	return malloc(size);
}

void operator delete(void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete [] (void * ptr)
{
	if (ptr)
		free(ptr);
}

void operator delete(void * p, void * const b)
{

}

#endif	/* defined(USE_MEM_MGMT) */

std::string Uppercase(const std::string & str)
{
	std::string s = str;
	for (int i = 0; i < s.size(); i++)
		s[i] = std::toupper(s[i]);
	return s;
}

std::string Lowercase(const std::string & str)
{
	std::string s = str;
	for (int i = 0; i < s.size(); i++)
		s[i] = std::tolower(s[i]);
	return s;
}

std::string TrimWhiteSpace(const std::string & str)
{
	int wsf, wsb;
	wsf = str.find_first_not_of(WHITESPACE);
	wsb = str.find_last_not_of(WHITESPACE);
	if (wsf == std::string::npos) {
		wsf = 0;
		wsb = -1;
	}
	return str.substr(wsf,wsb - wsf + 1);
}

int Meters2Feet(int m)
{
    return (m*3.281);
}

int Feet2Meters(int f)
{
    return (f/3.281);
}

// Stolen from Tab.cpp @ COCO/R source by Hanspeter Moessenboeck
char    Hex2Char(const char* s) {
    int val = 0;
    int len = std::strlen(s);
    for (int i = 0; i < len; i++) {
        char ch = s[i];
        if ('0' <= ch && ch <= '9') val = 16 * val + (ch - '0');
        else if ('a' <= ch && ch <= 'f') val = 16 * val + (10 + ch - 'a');
        else if ('A' <= ch && ch <= 'F') val = 16 * val + (10 + ch - 'A');
        else {
            ;
        }
    }
    if (val >= 3) {/* pdt */
        ;
    }
    return (char) val;
}

char*   Char2Hex(const char ch) {
    char* format = new char[10];
    sprintf(format, "0x%04x", ch);
    return format;
}

